// pages/midautumnteacher/midautumnteacher.js
import http from '../../utils/request'
import md5 from '../../utils/md5.js';//支付页面引入md5.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:null,
        sessionInfo:'',
        intergal:'',
        isShow:false,
        zqIshow:false,
        url:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //判断是否登录
        // let userInfo = JSON.parse(wx.getStorageSync('userInfo'))
        // // let opses = JSON.parse(wx.getStorageSync('third_Session')) || null
        // this.setData({userInfo:userInfo})
    //      let userInfo = wx.getStorageSync('userInfo') || null
    //   if(userInfo==null){
    //     this.setData({isShow:true})
    //   }else{
    },
    //点击充值
    async resouceslink(e){
        console.info(e)
        let money = e.currentTarget.dataset.money
        let intergral = e.currentTarget.dataset.jifen
        this.setData({url:e.currentTarget.dataset.url?e.currentTarget.dataset.url:''})
        let userInfo = wx.getStorageSync('userInfo') || null
        let userInfo1 = JSON.parse(wx.getStorageSync('userInfo'))
        if(userInfo==null){
            console.info('ddd')
            this.setData({isShow:true})
        }else{
            console.info('sss')
            let ordernum = new Date().getTime()+'_'+JSON.parse(wx.getStorageSync('userInfo')).id+'_'+intergral
            wx.setStorageSync('orderNumber', ordernum)
            let that = this
            let options={
                    url:'/WechatProgramPayForm',
                    method:'POST',
                    data:{
                        orderNumber: ordernum,
                        orderTitle: '积分充值',
                        orderAmount: money * 100,
                        openId:userInfo1.openId
                    },
                    header:{"content-type":"application/json;charset=UTF-8"}
                }
            var res = await http(options)
            if(res.data.code == 0){
                var payInfo = res.data.obj.split(',')
                var newpayinf = []
                payInfo.map(item=>{
                    newpayinf.push(item.replace('=',':'))
                })
                var timestamp = newpayinf[2].split(':')[1]
                var prepayid = newpayinf[3].replace(':','=').trim()
                var noncestr = newpayinf[6].split(':')[1]
                var stringA =`appId=wx91b2bce393fa3a10&nonceStr=${noncestr}&package=${prepayid}&signType=MD5&timeStamp=${timestamp}`
                var stringSignTemp=stringA+"&key=BBF522F0E6974DAE834977441495C11C" //注：key为商户平台设置的密钥key
                let signs=md5.hexMD5(stringSignTemp).toUpperCase() //注：MD5签名方式
                wx.requestPayment(
                {
                    "timeStamp":timestamp,
                    "nonceStr":noncestr,
                    "package":prepayid,
                    "signType":"MD5",
                    "paySign":signs,
                    "appId":"wx91b2bce393fa3a10",
                    "success":function(res){
                        if(e.currentTarget.dataset.url){
                            that.setData({zqIshow:true})
                            // wx.showToast({
                            //     title: '微信支付成功',
                            //     icon: 'success',
                            //     duration: 2000,
                            //     success:function(){
                            //         setTimeout(function(){
                            //             // let link = `https://pan.baidu.com/s/16jzaYzyckC557oe4EY_wRw?pwd=0y3p`
                            //             let link = e.currentTarget.dataset.url
                            //             wx.navigateTo({
                            //                 url: '/pages/webview/webview?url='+encodeURIComponent(link),
                            //             })
                            //         },2000);
                            //     }
                            // })
                        }else{
                            wx.showToast({
                                title: '微信支付成功',
                                icon: 'success',
                                duration: 2000,
                                // success:function(){
                                //     setTimeout(function(){
                                //         wx.reLaunch({
                                //             url:"/pages/mine/mine"
                                //         })
                                //     },2000);
                                // }
                            })
                        }
                    },
                    "fail":function(res){
                        wx.showToast({
                            title: '微信支付失败',
                            icon: 'error',
                            duration: 2000
                    })
                    }
                })
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
            })
            }
        }
    },
    dowToast(){
        let link = this.data.url
        wx.navigateTo({
            url: '/pages/saveemail/saveemail?url='+link+'&mark=中秋',
        })
    },
    downPPT(){
        let link = 'https://oss.bslmedu.com/wechart/active/645PPT.rar'
        wx.navigateTo({
            url: '/pages/saveemail/saveemail?url='+link+'&mark=中秋',
        })
    },
    closeTbad(){
        this.setData({zqIshow:false})
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})